{
 "cells": [
  {
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-11-02T06:00:28.394778Z",
     "start_time": "2025-11-02T06:00:28.392144Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from tokenize import group\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np"
   ],
   "id": "4b66642fd6ab50e0",
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T06:00:29.473593Z",
     "start_time": "2025-11-02T06:00:29.441784Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data = np.arange(12).reshape((3,4))\n",
    "df = pd.DataFrame(data,index=['a','b','c'],columns=['aa','bb','cc','dd'])\n",
    "df"
   ],
   "id": "9fa7d356cb32f487",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   aa  bb  cc  dd\n",
       "a   0   1   2   3\n",
       "b   4   5   6   7\n",
       "c   8   9  10  11"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>aa</th>\n",
       "      <th>bb</th>\n",
       "      <th>cc</th>\n",
       "      <th>dd</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T06:00:50.560791Z",
     "start_time": "2025-11-02T06:00:50.542543Z"
    }
   },
   "cell_type": "code",
   "source": "df.sort_index(axis=1)",
   "id": "23e9ed6b4b943108",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   aa  bb  cc  dd\n",
       "a   0   1   2   3\n",
       "b   4   5   6   7\n",
       "c   8   9  10  11"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>aa</th>\n",
       "      <th>bb</th>\n",
       "      <th>cc</th>\n",
       "      <th>dd</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T06:01:28.481486Z",
     "start_time": "2025-11-02T06:01:28.463456Z"
    }
   },
   "cell_type": "code",
   "source": "df.sort_values(by='cc')",
   "id": "d116937276aa6c15",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   aa  bb  cc  dd\n",
       "a   0   1   2   3\n",
       "b   4   5   6   7\n",
       "c   8   9  10  11"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>aa</th>\n",
       "      <th>bb</th>\n",
       "      <th>cc</th>\n",
       "      <th>dd</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>a</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>b</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>c</th>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T06:03:06.459586Z",
     "start_time": "2025-11-02T06:03:06.451090Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.DataFrame({ \"A\": [\"cat\", \"dog\", \"cat\", \"dog\", \"cat\", \"dog\", \"cat\", \"cat\"],\n",
    "       \"B\": [\"one\", \"one\", \"two\", \"three\", \"two\", \"two\", \"one\", \"three\"],\n",
    "       \"C\": np.arange(0,8),\n",
    "       \"D\": np.arange(8,0,-1)\n",
    "    } )\n",
    "df"
   ],
   "id": "461cbfefbdd04439",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "     A      B  C  D\n",
       "0  cat    one  0  8\n",
       "1  dog    one  1  7\n",
       "2  cat    two  2  6\n",
       "3  dog  three  3  5\n",
       "4  cat    two  4  4\n",
       "5  dog    two  5  3\n",
       "6  cat    one  6  2\n",
       "7  cat  three  7  1"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>cat</td>\n",
       "      <td>one</td>\n",
       "      <td>0</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>dog</td>\n",
       "      <td>one</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>cat</td>\n",
       "      <td>two</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>dog</td>\n",
       "      <td>three</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>cat</td>\n",
       "      <td>two</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>dog</td>\n",
       "      <td>two</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>cat</td>\n",
       "      <td>one</td>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>cat</td>\n",
       "      <td>three</td>\n",
       "      <td>7</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T06:04:27.084788Z",
     "start_time": "2025-11-02T06:04:27.079255Z"
    }
   },
   "cell_type": "code",
   "source": [
    "grouped = df.groupby(\"A\")\n",
    "grouped"
   ],
   "id": "b82482c34e847a14",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pandas.core.groupby.generic.DataFrameGroupBy object at 0x000002D4210F3750>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T06:05:35.482575Z",
     "start_time": "2025-11-02T06:05:35.476834Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(type(grouped))\n",
    "grouped = df.groupby([\"A\",\"B\"])\n",
    "grouped"
   ],
   "id": "8d7247eee49f3507",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.groupby.generic.DataFrameGroupBy'>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<pandas.core.groupby.generic.DataFrameGroupBy object at 0x000002D421214410>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T06:07:33.721728Z",
     "start_time": "2025-11-02T06:07:33.710735Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.DataFrame({ \"A\": [\"cat\", \"dog\", \"cat\", \"dog\", \"cat\", \"dog\"],\n",
    "                    \"B\": [1,np.nan,2,4,5,6]    } )\n",
    "print(df)\n",
    "print('使用列\\'A\\'分组后的size()方法结果：\\n',df.groupby(\"A\").size())\n",
    "print('使用列\\'A\\'分组后的count()方法结果：\\n',df.groupby(\"A\").count())\n",
    "print('使用列\\'A\\'分组后的prod()方法结果：\\n',df.groupby(\"A\").prod())"
   ],
   "id": "b831d081e7822ad4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     A    B\n",
      "0  cat  1.0\n",
      "1  dog  NaN\n",
      "2  cat  2.0\n",
      "3  dog  4.0\n",
      "4  cat  5.0\n",
      "5  dog  6.0\n",
      "使用列'A'分组后的size()方法结果：\n",
      " A\n",
      "cat    3\n",
      "dog    3\n",
      "dtype: int64\n",
      "使用列'A'分组后的count()方法结果：\n",
      "      B\n",
      "A     \n",
      "cat  3\n",
      "dog  2\n",
      "使用列'A'分组后的prod()方法结果：\n",
      "         B\n",
      "A        \n",
      "cat  10.0\n",
      "dog  24.0\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-02T06:07:49.165432Z",
     "start_time": "2025-11-02T06:07:49.153787Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print('在行上进行聚合\\n',df['B'].agg(['count','mean']))\n",
    "print('每列上使用不同的聚合方法\\n',df.agg({'A' : ['count', 'size'], 'B' : ['mean','count']}))\n",
    "print('对分组结果进行聚合\\n',df.groupby('A')['B'].agg(['sum', 'min']))"
   ],
   "id": "39e1457433f999d3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "在行上进行聚合\n",
      " count    5.0\n",
      "mean     3.6\n",
      "Name: B, dtype: float64\n",
      "每列上使用不同的聚合方法\n",
      "          A    B\n",
      "count  6.0  5.0\n",
      "size   6.0  NaN\n",
      "mean   NaN  3.6\n",
      "对分组结果进行聚合\n",
      "       sum  min\n",
      "A             \n",
      "cat   8.0  1.0\n",
      "dog  10.0  4.0\n"
     ]
    }
   ],
   "execution_count": 14
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
